﻿using System;
using System.Drawing;
using System.IO.Ports;
using System.Media;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace 物奇标签验证
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private SerialPort port1 = new SerialPort();
        private SoundPlayer playerNG = new SoundPlayer(System.Environment.CurrentDirectory + "\\ng.wav");
        private SoundPlayer playerOK = new SoundPlayer(System.Environment.CurrentDirectory + "\\ok.wav");
        private bool noCheck = true;
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            label3.Text = "";
            if (e.KeyValue == 13)
            {
                textBox1.BackColor = Color.White;
                if (comboBox1.Text == "")
                {
                    label3.Text = "未选择串口！";
                    return;
                }
                if (port1.IsOpen)
                {
                    port1.DiscardInBuffer();
                    port1.Close();
                }
                if (!OpenPort(port1, comboBox1.Text, 9600))
                {
                    label3.Text = "串口打开失败！";
                    return;
                }
                port1.DiscardInBuffer();
                string reData = GetData(port1).Replace("\u0002", "").Replace("\u0003", "").Replace("\r\n", "");
                
                //reData = reData.Substring(0, reData.Length-2);
                if (!reData.Equals(textBox1.Text) && reData.Length == 0)
                {
                    reData = GetData(port1).Replace("\u0002", "").Replace("\u0003", "").Replace("\r\n", "");
                }
                if (!reData.Equals(textBox1.Text) && reData.Length == 0)
                {
                    reData = GetData(port1).Replace("\u0002", "").Replace("\u0003", "").Replace("\r\n", "");
                }
                textBox2.Text = reData.Trim();
                if (reData.Length != 0&&reData.Equals(textBox1.Text.Trim())  )
                {
                    textBox1.BackColor = Color.Lime;
                    label3.Text = "验证成功";
                    textBox1.Clear();
                    playerOK.Play();
                }
                else
                {
                    textBox1.BackColor = Color.Red;
                    label3.Text = "条码与标签信息不一致！";
                    textBox1.SelectAll();
                    playerNG.Play();
                }
                port1.DiscardInBuffer();
                port1.Close();
            }
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(SerialPort.GetPortNames());
        }
        public bool OpenPort(SerialPort serialPort, string port, int baudRate)
        {
            try
            {
                serialPort.PortName = port;
                serialPort.BaudRate = baudRate;
                serialPort.Parity = Parity.None;
                serialPort.DataBits = 8;
                serialPort.StopBits = StopBits.One;
                if (!serialPort.IsOpen)
                {
                    serialPort.Open();
                }
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show("打开串口失败！");
                return false;
            }

        }
        public void Delay(int Millsecond)
        {
            DateTime current = DateTime.Now;
            while (current.AddMilliseconds(Millsecond) > DateTime.Now)
            {
                Thread.Sleep(10);
                Application.DoEvents();
            }
            return;
        }
        public string GetData(SerialPort port)
        {
            try
            {
                string reData = "";
                while (true)
                {
                    Delay(100);
                    if (port.BytesToRead > 1)
                    {
                        Delay(100);
                        byte[] recData = new byte[port.BytesToRead];
                        port.Read(recData, 0, recData.Length);
                        reData = Encoding.Default.GetString(recData);
                        return reData;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return e.Message.ToString();
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(SerialPort.GetPortNames());
        }

        private void label3_TextChanged(object sender, EventArgs e)
        {
            if (label3.Text.Contains("！"))
            {
                label3.ForeColor = Color.Red;
            }
            else
            {
                label3.ForeColor = Color.Black;
            }
        }
    }
}
